package com.logic.landseaserver.ws;


import java.util.HashMap;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.domain.ReportContact;
import com.logic.landseaserver.service.IReportContactService;
import com.logic.landseaserver.service.impl.ReportContactServiceImpl;
import com.logic.landseaserver.ws.dto.ReportContactDTO;
import com.logic.landseaserver.ws.dto.ReportContactPdfDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Path("/reportContract")
@Api(value="reportContract")
public class ReportContactRest {
	
	@Autowired
	private IReportContactService reportContactService;

	@Autowired
	private ReportContactServiceImpl reportContactServiceImpl;
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	//工程联系单
	@POST
	@Path("/reportContract/list")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list reportContract")
	public Response reportContract(@ApiParam(value = "param", required = true) @QueryParam("reportProjectId") Integer id){
		CommonResultMap map = new CommonResultMap();
		try {
			List<ReportContactDTO> reportContacts = reportContactService.queryReportContact(id);
			map.setData(reportContacts);
			map.setResult(CommonResult.SUCCESS);
		} catch (Exception e) {
		    logger.error("reportContract error.", e);
			map.setResult(CommonResult.ERROR, e.getMessage());
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	
	@POST
	@Path("/reportContract")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list reportContract")
	public Response reportContractDTO(@ApiParam(value = "param", required = true) @QueryParam("Id") Integer id){
		CommonResultMap map = new CommonResultMap();
		try {
			ReportContact reportContact = (ReportContact)reportContactServiceImpl.getObject(id);
			map.setData(reportContact);
			map.setResult(CommonResult.SUCCESS);
		} catch (Exception e) {
		    logger.error("reportContractDTO error.", e);
			map.setResult(CommonResult.ERROR, e.getMessage());
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	@POST
	@Path("/creat/reportContract")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new reportContract")
	public Response createReportContract(@ApiParam(value = "param", required = true) ReportContact reportContact) {
		CommonResultMap map = new CommonResultMap();
		try {
			int id = reportContactService.insertReportContact(reportContact);
			reportContact.setId(id);
            HashMap<String ,Integer > resultmap = new HashMap<String ,Integer>();
            resultmap.put("id", id);
            map.setData(resultmap);
			map.setResult(CommonResult.SUCCESS, "Create success");
		} catch (BusinessException e) {
		    logger.error("createReportContract error.", e);
			map.setResult(e);
		}  catch (Exception e) {
		    logger.error("createReportContract error.", e);
			map.setResult(CommonResult.ERROR, e.getMessage());
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	@DELETE
	@Path("/delete/reportContract")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "logic delete reportContract by set deleted")
	public Response logicDelete(@ApiParam(value = "param", required = true) @QueryParam("reportContractId") String ids) {
		CommonResultMap map = new CommonResultMap();
		try {
			reportContactServiceImpl.delete(ids);
			map.setResult(CommonResult.SUCCESS,"delete reportStrip");
		} catch (Exception e) {
		    logger.error("logicDelete error.", e);
			map.setResult(CommonResult.ERROR, e.getMessage());
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
    //获取pdf详情
    @GET
    @Path("/getPdfDitail/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get reportContact pdf detail ", position = 4)
    public Response getPdfDeatilById(
          @ApiParam(value = "param", required = true) @PathParam("id") Integer id) {
    	CommonResultMap map = new CommonResultMap();
    	try {
    		map.setData(reportContactService.getPdfDitail(id));
    	} catch (Exception e) {
    	    logger.error("getPdfDeatilById error.", e);
    	}
    	return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
	
	//生成pdf
    @PUT
    @Path("/reportContactPDF")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "生成pdf", position = 12)
    public Response reportContactPDF(
            @ApiParam(value = "param", required = true) @QueryParam("id") int id) {
        CommonResultMap map = new CommonResultMap();
        String  filePath = null;
        try {
        	ReportContactPdfDTO reportContact = reportContactService.getPdfDitail(id);
        	//reportContact.setId(id);
        	filePath = reportContactService.reportContactPdf(reportContact);
            HashMap resultmap = new HashMap();
            resultmap.put("filePath", filePath);
            map.setData(resultmap);
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaBusinessException e) {
            logger.error("reportContactPDF error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("reportContactPDF error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

	
}
